package com.hzqy.service.statistics;

import com.hzqy.persistence.dao.statistics.StatDataQxVisitDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.statistics.QxServerIdVisitVo;
import com.hzqy.web.vo.statistics.StatDataQxVisitVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StatDataQxVisitServiceBean extends GenericServiceBean<StatDataQxVisitVo, StatDataQxVisitDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	@Resource
	private StatDataQxVisitDao statDataQxVisitDao;
	
	@Override
	public StatDataQxVisitDao getEntityDao() {
		return statDataQxVisitDao;
	}

	/**
	 * 根据条件统计查询区县UV和PV数据
	 * @param statDataQxVisitVo
	 * @param curPage
	 * @param pageSize
	 * @return
	 */
	public Map<String, Object> selectStatDataQxVisitList(StatDataQxVisitVo statDataQxVisitVo, int curPage, int pageSize) {
		Map<String,Object> map = new HashMap<String, Object>();

		map.put("f_sdqv_date",statDataQxVisitVo.getF_sdqv_date());
		map.put("f_sdqv_qxserverid",statDataQxVisitVo.getF_sdqv_qxserverid());

		Integer rowCount = 0;
		rowCount = statDataQxVisitDao.selectStatDataQxVisitCount(map);
		map.put("total", rowCount);

		if(rowCount > 0 && curPage > 0 && pageSize > 0) {
			map.put("firstResult", (curPage-1)*pageSize);
			map.put("maxResults", pageSize);

			List<StatDataQxVisitVo> results = new ArrayList<StatDataQxVisitVo>();
			results = statDataQxVisitDao.selectStatDataQxVisitList(map);
			map.put("rows", results);
		} else {
			map.put("rows", new ArrayList<StatDataQxVisitVo>());
		}

		return map;
	}

	public Map<String, Object> selectStatDataQxVisitExcelList(StatDataQxVisitVo statDataQxVisitVo) {
		Map<String,Object> map = new HashMap<String, Object>();

		map.put("f_sdqv_date",statDataQxVisitVo.getF_sdqv_date());
		map.put("f_sdqv_qxserverid",statDataQxVisitVo.getF_sdqv_qxserverid());

		List<StatDataQxVisitVo> results = new ArrayList<StatDataQxVisitVo>();
		results = statDataQxVisitDao.selectStatDataQxVisitExcelList(map);

		if(results.isEmpty()){
			map.put("rows", new ArrayList<StatDataQxVisitVo>());
		}else{
			map.put("rows", results);
		}

		return map;
	}

	public Map<String, Object> selectQxServerIdVisitExcelList(QxServerIdVisitVo qxServerIdVisitVo) {
		Map<String,Object> map = new HashMap<String, Object>();

		String selectDate = qxServerIdVisitVo.getSelectDate();
		if(selectDate==null||selectDate.length()<=0){
			map.put("rows", new ArrayList<QxServerIdVisitVo>());
		}else{
			String databaseNameDate = selectDate.substring(0,4)+selectDate.substring(5,7);
			String databaseName = "itv_weblogs.qx_serverid_visit_"+databaseNameDate;
			map.put("databaseName",databaseName);
			map.put("f_qsv_cdate",selectDate);
			map.put("f_qsv_qx_serverid",qxServerIdVisitVo.getF_qsv_qx_serverid());

			List<QxServerIdVisitVo> results = new ArrayList<QxServerIdVisitVo>();
			results = statDataQxVisitDao.selectQxServerIdVisitExcelList(map);

			if(results.isEmpty()){
				map.put("rows", new ArrayList<QxServerIdVisitVo>());
			}else{
				map.put("rows", results);
			}
		}
		return map;
	}

}
